/*
 * @# shows.js 2011-12-28 上午09:36:31
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
/**
 * 秀场.
 */
$(function() {
	$('div.content').css('display', 'block');
	var onMouseOutOpacity = 0.6; //0.67;
	$('#thumbs ul.thumbs li, div.navigation a.pageLink').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected1'
	});
	
	var gallery = $('#thumbs').galleriffic({
		delay:                     2500,
		numThumbs:                 6,//缩略图显示数
		preloadAhead:              6,//预加载图片数
		enableTopPager:            false,//是否启用上翻页
		enableBottomPager:         false,//是否启用下翻页
		imageContainerSel:         '#slideshow',//大图显示容器
		controlsContainerSel:      '#controls',//播放、上一图、下一图
		captionContainerSel:       '#caption',//图片文字说明区
		loadingContainerSel:       '#loading',//正在载入gif图像
		renderSSControls:          false,//播放、暂停
		renderNavControls:         false,//上一图、下一图
		playLinkText:              '自动播放',//播放显示文字
		pauseLinkText:             '暂停播放',//暂停显示文字
		prevLinkText:              '&lsaquo; 上一张',//上一图显示文字
		nextLinkText:              '下一张 &rsaquo;',//下一图显示文字
		nextPageLinkText:          '下一页 &rsaquo;',//下一页显示文字
		prevPageLinkText:          '&lsaquo; 上一页',//上一页显示文字
		enableHistory:             true,//是否启用后退
		autoStart:                 false,//是否启用自动播放
		syncTransitions:           true,//是否启用同步显示
		defaultTransitionDuration: 900,//
		onSlideChange: function(prevIndex, nextIndex) {
			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
			
			this.$captionContainer.find('div.photo-index span')
				.html('(<span class="forthis"> '+ (nextIndex+1) +'</span> / '+ this.data.length + ' )');
		},
		onPageTransitionOut:  function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			var prevPageLink = this.find('a.prev').css('visibility', '');
			var nextPageLink = this.find('a.next').css('visibility', '');
			if (this.displayedPage > 0) {
				prevPageLink.css('visibility', 'visible');
			}
			var lastPage = this.getNumPages() - 1;
			if (this.displayedPage < lastPage) {
				nextPageLink.css('visibility', 'visible');
			}
			this.fadeTo('fast', 1.0);
		}
	});

	/**************************************/

	gallery.find('a.prev').click(function(e) {
		gallery.previousPage();
		e.preventDefault();
	});

	gallery.find('a.next').click(function(e) {
		gallery.nextPage();
		e.preventDefault();
	});

	/*************************************/
	function pageload(hash) {
		if(hash) {
			$.galleriffic.gotoImage(hash);
		} else {
			gallery.gotoIndex(0);
		}
		
		changePhoto(hash);
	}
	$.historyInit(pageload, "advanced.html");
	$("a[rel='history']").live('click', function(e) {
		if (e.button != 0) return true;
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});
	
	// -----------------------------------------
	var initPhotoId = $('#init-photo-id').val()
		initHash = '#' + initPhotoId;
	if (!window.location.hash) {
		window.location.hash = initHash;
		pageload(initPhotoId);
	}
	
	var currentUserId = $('#current-user-id').val(), currentUserAvatar = $('#current-user-avatar').val();
	$('#like-photo-btn').click(function(e) {
		var albumId = $('#album-id').val();
		var photoId = $('#photo-id').val();
		$.post(window.ctx + '/albums/' + albumId + '/photos/' + photoId + '/likes/create', function(feedback) {
			if (feedback.successful) {
				$('#like-photo-text').text('已赞过');
				var num = parseInt($('#like-photo-num').val() || 0);
				$('#like-photo-num').val(num + 1);
				if (num < 20) {
					var html = '<a title="' + currentUserId + '" href="/u/"' + currentUserId + '>' + 
								'<img src="' + Px.Avatar.getS40Url(currentUserAvatar) + '" class="usercard">' +
							'</a>';
					$('#likes-user-list').append(html);
				}
			}
		});
	});
	// 收藏
	$('#favor-photo-btn').click(function(e) {
		var albumId = $('#album-id').val();
		var photoId = $('#photo-id').val();
		$.post(window.ctx + '/albums/' + albumId + '/favors/photos/' + photoId + '/create', function(feedback) {
			if (feedback.successful) {
				$('#favor-photo-text').text('已收藏');
				var num = parseInt($('#favor-photo-num').val() || 0);
				$('#favor-photo-num').val(num + 1);
			}
		});
	});
	
	function changePhoto(photoId) {
		$('#photo-id').val(photoId);
		
		var albumId = $('#album-id').val();
		loadPhotoInfo(albumId, photoId);
		
		loadLikesUsers(albumId, photoId);
	}
	
	function loadPhotoInfo(albumId, photoId) {
		$('#favor-photo-text').text('收藏');
		$('#like-photo-text').text('赞');
		$.getJSON(window.ctx + '/albums/' + albumId + '/photos/' + photoId + '/info', function(photo) {
			if (photo.favors) {
				$('#favor-photo-num-wrapper').html('(<span class="tcolor" id="favor-photo-num">' + photo.favors + '</span>)');
			} else {
				$('#favor-photo-num-wrapper').html('');
			}
		});
	}
	function loadLikesUsers(albumId, photoId) {
		// 喜爱
		$.getJSON(window.ctx + '/albums/' + albumId + '/photos/' + photoId + '/likes', function(likes) {
			var html = '';
			$.each(likes, function(i, like) {
				var user = like.user;
				html += '<a title="' + user.nickname + '" href="/u/' + user.id + '">' + 
							'<img src="' + Px.Avatar.getS40Url(user.avatar) + '" class="usercard">' +
						'</a>';
			});
			if (html) {
				$('#likes-user-list').html(html);
				$('#likes-content').removeClass('hidden').show();
			}
		});
	}
});
